package com.program.beautymakeup.service;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import com.program.beautymakeup.controller.form.UploadPortfolioForm;
import com.program.beautymakeup.domain.entity.Portfolio;
import com.program.beautymakeup.domain.entity.PortfolioImg;
import com.program.beautymakeup.domain.vo.ResultInfo;
import com.program.beautymakeup.mapper.PortfolioImgMapper;
import com.program.beautymakeup.mapper.PortfolioMapper;
import com.program.beautymakeup.utils.AliOssUtils;
import com.program.beautymakeup.utils.ResultInfoUtil;
import com.program.beautymakeup.utils.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @author bytedance
 * @ClassName UploadService.java
 * @Description TODO
 * @createTime 2023年02月07日 00:11:00
 */
@Service
@Slf4j
public class UploadService {
    //获取根路径
    private String dirPath = "../static/";

//    @Autowired
//    private UploadMapper uploadMapper;
    @Autowired
    private AliOssUtils aliOssUtils;

    @Autowired
    private PortfolioMapper portfolioMapper;

    @Autowired
    private PortfolioImgMapper portfolioImgMapper;
    @Autowired
    private FileStorageService fileStorageService;

    public String uploadImg(List<MultipartFile> fileList) {
        Boolean img =UploadUtil.checkImg(fileList);
        StringBuilder URL = new StringBuilder();
        if (img){
//            String URL= UploadUtil.filesUpload(fileList,request, Constraints.DIRPATH);
            for (MultipartFile multipartFile : fileList) {
                long start = System.currentTimeMillis();
//                FileInfo upload  = fileStorageService.of(multipartFile).setPlatform("aliyun-oss").upload();
//                String itemUrl = upload.getUrl();
                String itemUrl = aliOssUtils.uploadFileAvatar(multipartFile);
                log.info("[上传文件成功],itemUrl:{},file_name:{},file_size:{},cost_time:{}",itemUrl,multipartFile.getOriginalFilename(),multipartFile.getSize(),System.currentTimeMillis()-start);
                if (URL.toString().equals("")) {
                    URL = new StringBuilder(itemUrl);
                }else {
                    URL.append(",").append(itemUrl);
                }
            }
            return URL.toString();
        }else {
            return "请上传bmp/gif/jpg/png格式的图片";
        }
    }

    public ResultInfo uploadPortfolios(UploadPortfolioForm data) {
        Date curTime = new Date();
        Portfolio portfolio = new Portfolio();
        portfolio.setName(data.getName());
        portfolio.setType(data.getType());
        portfolio.setUserId(data.getUserId());
        portfolio.setUpdateTime(curTime);
        portfolio.setCreateTime(curTime);
        portfolioMapper.insert(portfolio);

        Integer portfolioId = portfolio.getId();
        List<PortfolioImg> portfolioImgList = new ArrayList<>();
        List<String> fileUrlList = new ArrayList<>();
//        保存每一个文件上传后的url,格式为/upload/，不包含ip地址以及端口
        if (data.getFileList() != null) {
            for (MultipartFile file : data.getFileList()) {
                PortfolioImg item = new PortfolioImg();
                item.setPortfolioId(portfolioId);
                item.setCreateTime(curTime);
//            String fileUrl = Constraints.ADDRESS + UploadUtil.upload(file, Constraints.DIRPATH);
                String fileUrl = aliOssUtils.uploadFileAvatar(file);
                item.setUrl(fileUrl);
                fileUrlList.add(fileUrl);

                portfolioImgList.add(item);
            }
        }

        for (String fileUrl : data.getFileUrls()) {
            PortfolioImg item = new PortfolioImg();
            item.setPortfolioId(portfolioId);
            item.setCreateTime(curTime);
            item.setUrl(fileUrl);
            fileUrlList.add(fileUrl);

            portfolioImgList.add(item);
        }

        portfolioImgMapper.batchInsertPortfolioImg(portfolioImgList);

        Map<String, Object> res = new HashMap();
        res.put("portfolio", portfolio);
        res.put("urls", fileUrlList);

        return ResultInfoUtil.buildSuccess(res);
    }

    public ResultInfo deleteFile(List<String> urls) {
        for (String url : urls) {
//            Boolean rsp = UploadUtil.deleteFile(Constraints.DIRPATH, url);
            aliOssUtils.deleteFile(url);
        }
        return ResultInfoUtil.buildSuccess("删除成功");
    }
}
